package middleware

import (
    "github.com/gin-gonic/gin"
    "net/http"
    "server/app"
)

// 安全验证中间件
func ValidMiddleware(skipper ...SkipperFunc) gin.HandlerFunc {
    return func(c *gin.Context) {
        if len(skipper) > 0 && skipper[0](c) {
            c.Next()
            return
        }

        token := c.GetHeader("X-Request-Token")
        if token != app.GetConfig().Token {
            c.JSON(http.StatusOK, app.NewRest().Code(401).Msg("未授权"))
            c.Abort()
            return
        }
        c.Next()

        //if c.Request.Header.Get("app")!="app" {
        //	c.AbortWithError(500,errors.New("无权限"))
        //	return
        //}
        //c.Next()

        //fmt.Println(c.Request.URL.String())
        //if cookie, err := c.Request.Cookie("session_id"); err == nil {
        //	value := cookie.Value
        //	fmt.Println(value)
        //	if value == "onion" {
        //		c.Next()
        //		return
        //	}
        //}
        //if url := c.Request.URL.String(); url == "/login" {
        //	c.Next()
        //	return
        //}
        //c.JSON(http.StatusOK, app.JsonResult{Code: 401, Msg: "未授权"})
        //c.Abort()
    }
}
